#include <iostream>
#include <vector>
#include <Ice/Ice.h>
#include "Receiver.h"
#include <fstream>

using namespace std;
using namespace cap;
using namespace Ice;

class CaptureReceiver : public cap::Receiver
{
public:
	virtual void record(const DataArray &data, const Current& current)
	{
		cout << "Function received sequence count: " << data.size() << endl;

		ofstream outFile("image.png", ios::binary);
		for (DataArray::const_iterator iter = data.begin(); iter != data.end(); iter++)
			outFile << *iter;
		outFile.flush();
		outFile.close();
	}
};

class CaptureServer : public Ice::Application
{
public:

	virtual int run(int, char*[])
	{
		Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Hello");
		ReceiverPtr receiver = new CaptureReceiver();
		adapter->add(receiver, communicator()->stringToIdentity("capture"));
		adapter->activate();
		communicator()->waitForShutdown();
		return EXIT_SUCCESS;
	}
};

int main(int argc, char* argv[])
{
	CaptureServer app;
	return app.main(argc, argv, "config.server");
}
